package control;

import database.Database;
import java.sql.SQLException;
import model.User;

/**
 * Denne klasse håndterer manipulation af User
 * @author Sonny Sandberg
 */
public abstract class UserControl 
{    
    /**
     * Tilføj Bruger
     * @param User
     * @return int
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    public static int addUser(User user) throws ClassNotFoundException, SQLException
    {
        Database db = new Database();
        int added = db.manipulateDB("INSERT INTO bs_user "
                + "(username, password, email, securitykey, access_level)"
                + " VALUES "
                + "('"+user.getUsername()+"', '"+user.getPassword()+"', '"+user.getEmail()+"', '"+user.getSecuritykey()+"', "+user.getAccessLevel()+")");
        db.closeDB();
        
        return added;
    }
    
    /**
     * Opdater Bruger
     * @param User
     * @return int
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    public static int updateUser(User user) throws ClassNotFoundException, SQLException
    {
        Database db = new Database();
        int altered = db.manipulateDB("UPDATE bs_user SET username = '"+user.getUsername()+"', email = '"+user.getEmail()+"', "
                + "                    password = '"+user.getPassword()+"', access_level = "+user.getAccessLevel()+"  WHERE id = "+user.getId());
        db.closeDB();
        
        return altered;
    }
}